今天我要介紹的功能是摩斯密碼轉換,這個功能能夠將文字訊息轉換成摩斯密碼,同時也能將摩斯密碼解碼回原始文字。
摩斯密碼是一種歷史悠久的編碼方式,它以一系列的點(.)和線(-)來表示不同的字母和符號,曾被廣泛應用於電報通信和傳輸。實現這樣的功能並不複雜,現在就開始介紹程式碼。
public Map<String, String> getMorseCodeList() {
// Basic Latin alphabet
morseCode.put("a", ".-");
morseCode.put("b", "-...");
morseCode.put("c", "-.-.");
morseCode.put("d", "-..");
morseCode.put("e", ".");
morseCode.put("f", "..-.");
morseCode.put("g", "--.");
morseCode.put("h", "....");
.....
return morseCode;
}
public Map<String, String> getEnglishToMorseCode() {
morseCodeReversed.put(".-", "a");
morseCodeReversed.put("-...", "b");
morseCodeReversed.put("-.-.", "c");
morseCodeReversed.put("-..", "d");
morseCodeReversed.put(".", "e");
morseCodeReversed.put("..-.", "f");
morseCodeReversed.put("--.", "g");
morseCodeReversed.put("....", "h");
morseCodeReversed.put("..", "i");
morseCodeReversed.put(".---", "j");
morseCodeReversed.put("-.-", "k");
......
return morseCodeReversed;
}
/**dash點擊*/
private void dashIconClicked(View view) {
morseCodeBuilder.append("-");
input_editText.setText(morseCodeBuilder);
}
/**slash點擊*/
private void slashIconClicked(View view) {
morseCodeBuilder.append("/");
input_editText.setText(morseCodeBuilder);
}
/**dot點擊*/
private void dotIconClicked(View view) {
morseCodeBuilder.append(".");
input_editText.setText(morseCodeBuilder);
}
/**blank點擊*/
private void blankIconClicked(View view) {
morseCodeBuilder.append(" ");
input_editText.setText(morseCodeBuilder);
}
/**delete點擊*/
private void deleteIconClicked(View view) {
if(morseCodeBuilder.length() != 0) {
morseCodeBuilder.deleteCharAt(morseCodeBuilder.length() - 1);
input_editText.setText(morseCodeBuilder);
}
if(!isChange) {
StringBuilder text = new StringBuilder();
text.append(input_editText.getText().toString());
if(text.length() != 0) {
input_editText.setText(text.deleteCharAt(text.length() - 1));
input_editText.setSelection(text.length());
}
}
}
先介紹一半,明天會講解輸出的邏輯和其他使用到的小功能。